'use strict';


const Authorization = Symbol('Authorization');

module.exports = {
  /**
   * http request
   * @param {string} url 
   * @param {object} options 
   */
  async call(url, options) {
    try {
      let res = await this.curl(url, options);

      if (res.status == 200) {
        let data = res.data.toString('utf-8', 0, res.data.length);
        if (data.endsWith('}') && data.startsWith('{')) {
          return JSON.parse(data);
        }
        return data;
      } else {
        let detail = res.data.toString('utf-8', 0, res.data.length);
        this.throw(500,
          `Request: ${url}
          Status: ${res.status}
          Detail: ${detail.endsWith('}') && detail.startsWith('{') ? JSON.stringify(detail) : detail}`);
      }
    } catch (ex) {
      this.throw(500, ex.stack);
    }
  },
  clearToken() {
    this.cookies.set("token", null, {
      maxAge: 30 * 59 * 1000,
      signed: false,
      encrypt: false,
      httpOnly: false
    })
  },
  setToken(token) {
    this.cookies.set("token", token, {
      maxAge: 30 * 59 * 1000,
      signed: false,
      encrypt: false,
      httpOnly: false
    })
  }
};
